<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

	// form validation message
	public $m_strTopic				= "หัวข้อ";
	public $m_strBody				= "เนื้อหา";
	public $m_strInsertErrorMsg		= "ไม่สามารถเพิ่มข่าวได้";
	public $m_strRequired			= "กรุณากรอกข้อมูล %s";
	public $m_strMatches			= "ข้อมูลไม่ตรงกัน ";
	public $m_pagingSize 			= 10;

	public function __construct()
	{
		session_start();
		parent::__construct();
		$this->load->helper('form');

		$this->load->helper("url");
		$this->load->model("news_model");
		$this->load->library("pagination");

		if ( !isset($_SESSION['username']) && !isset($_SESSION['user_level']))
		{
			redirect('webadmin', 'refresh');
		}

	}

	public function index()
	{
		//paging
		$config["base_url"] = site_url() . "/news/news_list";
		$config["total_rows"] = $this->news_model->db_record_count();
		$config["per_page"] = $this->m_pagingSize;
		$this->pagination->initialize($config);
		$page = 0;

		$data["rs"] = $this->news_model->db_get_news($config["per_page"], $page);
		$data["links"] = $this->pagination->create_links();
 
		$this->load->view("news_list", $data);
		//end paging
	}

	public function news_list()
	{
		//paging
		$config["base_url"] = site_url() . "/news/news_list";
		$config["total_rows"] = $this->news_model->db_record_count();
		$config["per_page"] = $this->m_pagingSize;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
		$data["rs"] = $this->news_model->db_get_news($config["per_page"], $page);
		$data["links"] = $this->pagination->create_links();
 
		$this->load->view("news_list", $data);
		//end paging
	}

	public function add_news()
	{
		if ($_SESSION['user_level'] == "reader")
		{
			redirect("news", "refresh");
		}

		$this->load->library('form_validation');

		$frm = $this->form_validation;
		$frm->set_rules('topic', $this->m_strTopic, 'required|trim[255]');
		$frm->set_rules('body', $this->m_strBody, 'required|trim');
		$frm->set_message("required", $this->m_strRequired);
		$frm->set_message("matches", $this->m_strMatches);

		if ($frm->run() == FALSE)
		{
			$data["err_msg"] = NULL;
			$this->load->view('add_news', $data);
		}
		else
		{
			//echo "Form success validation";
			$topic = $this->input->post("topic");
			$body = $this->input->post("body");

			$res = $this->news_model->db_add_new_news_by_id($topic, $body);
			if ($res == TRUE)
			{
				redirect("news", "refresh");
			}
			else
			{
				$data["err_msg"] = $this->m_strInsertErrorMsg;
				$this->load->view('add_news', $data);
			}
		}
	}

	public function update_news($a_id)
	{
		if ($_SESSION['user_level'] == "reader")
		{
			redirect("news", "refresh");
		}

		$this->load->library('form_validation');

		$frm = $this->form_validation;
		$frm->set_rules('topic', $this->m_strTopic, 'required|trim[255]');
		$frm->set_rules('body', $this->m_strBody, 'required|trim');
		$frm->set_message("required", $this->m_strRequired);
		$frm->set_message("matches", $this->m_strMatches);

		// get data from selected item
		$row = $this->news_model->db_get_row_from_id($a_id);
		$data["row"] = $row;
		$data["err_msg"] = NULL;

		if ($frm->run() == FALSE)
		{
			$data["err_msg"] = NULL;
			$this->load->view('update_news', $data);
		}
		else
		{
			//echo "Form success validation";
			$topic = $this->input->post("topic");
			$body = $this->input->post("body");

			$res = $this->news_model->db_update_news($a_id, $topic, $body);
			if ($res == TRUE)
			{
				redirect("news", "refresh");
			}
			else
			{
				$data["err_msg"] = $this->m_strInsertErrorMsg;
				$this->load->view('update_news', $data);
			}
		}
	}

	public function delete_news()
	{
		if (isset($_SESSION['user_level']) != "admin")
		{
			redirect('welcome', 'refresh');
		}
				
		$chkbox = $this->input->post('mycheckbox');
		if ($chkbox)
		{
			foreach ($chkbox as $cb)
			{
				$this->news_model->db_delete_news($cb);
			}
		}
		redirect("news", "refresh");
	}

}
